local oo = require("SharedLibs.BaseLib")

Debugger = require("SharedLibs.Debugger");
TableUtil = require("SharedLibs.Utils.TableUtil");

local Map = oo.richClass("Map")

function Map:constructor()
	self._table = {};
	self._count = 0;
end

Map.property("count", 
	{
		getter = function(self)
			return self._count;
		end 
	});


Map.property("values", 
	{
		getter = function(self)
			return self._table;
		end 
	});

function Map:Clear()
	self._count = 0;
	for k, v in ipairs(self._table) do
		self._table[k] = nil;
	end
end

--- Add items to the end of an array
function Map:Add(key, value)
	self._table[key] = value;
	self._count = self._count + 1;
end

--- Remove an item from the end of an array
function Map:Remove(key)
	local old = self._table[key];
	self._table[key] = nil;
	self._count = self._count - 1;
	return old;
end

function Map:Get(key)
	return self._table[key];
end

--- 随机获取一个元素
function Map:RandomGet()
	local n = self._count;
	if n == 0 then return nil end;

	local stopIndex = math.random(1, n);

	local i = 0;	
	for k, v in pairs(self._table) do	
		i = i + 1;
		if i == stopIndex then
			return v, k;
		end
	end	
end

function Map:Contains(key)
	return self._table[key] ~= nil;
end

function Map:ForEach(func)
	for k, v in pairs(self._table) do
		func(k, v);
	end
end

return Map;
